package com.xilihui.xlh.business.dialogs;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

import com.xilihui.xlh.business.interfaces.OnDismissListener;
import com.xilihui.xlh.business.interfaces.OnNegativeClickListener;
import com.xilihui.xlh.business.interfaces.OnNeutralClickListener;
import com.xilihui.xlh.business.interfaces.OnPositiveClickListener;


/**
 * 2017/12/19.
 */
public class MaterialDialogFragment extends DialogFragment {

    public static final int CONFIRM = 0x100;
    public static final int CANCEL = 0x200;
    public static final int NEUTRAL = 0x300;

    private OnPositiveClickListener positiveClickListener;
    private OnNegativeClickListener negativeClickListener;
    private OnNeutralClickListener neutralClickListener;
    private OnDismissListener dismissListener;
    private MaterialDialogFragment fragment;

    private String positive = "确定";
    private String negative = "取消";
    private String neutral = "";
    private int target;// 点击的按钮

    public MaterialDialogFragment() {
        fragment = this;
    }

    public MaterialDialogFragment setButtonText(String positive, String negative) {
        this.positive = positive;
        this.negative = negative;
        return fragment;
    }

    public MaterialDialogFragment setButtonTextNeutral(String neutral) {
        this.neutral = neutral;
        return fragment;
    }

    public MaterialDialogFragment setOnPositiveClickListener(OnPositiveClickListener listener) {
        this.positiveClickListener = listener;
        return fragment;
    }

    public MaterialDialogFragment setOnNegativeClickListener(OnNegativeClickListener listener) {
        this.negativeClickListener = listener;
        return fragment;
    }

    public MaterialDialogFragment setOnDismissListener(OnDismissListener listener) {
        this.dismissListener = listener;
        return fragment;
    }

    public MaterialDialogFragment setOnNeutralClickListener(OnNeutralClickListener listener) {
        this.neutralClickListener = listener;
        return fragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String msg = getArguments().getString("msg");
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(msg)
                .setPositiveButton(positive, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        target = CONFIRM;
                        if (positiveClickListener != null) {
                            positiveClickListener.onClick();
                        }
                    }
                })
                .setNegativeButton(negative, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        target = CANCEL;
                        if (negativeClickListener != null) {
                            negativeClickListener.onClick();
                        }
                    }
                });
        if (neutralClickListener != null) {
            builder.setNeutralButton(neutral, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    target = NEUTRAL;
                    if (neutralClickListener != null) {
                        neutralClickListener.onClick();
                    }
                }
            });
        }
        return builder.create();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (dismissListener != null) {
            dismissListener.onDismiss(target);
        }
        super.onDismiss(dialog);
    }

    @Override
    public void onDestroyView() {
        fragment = null;
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        fragment = null;
        super.onDestroy();
    }
}
